from pos import Position,Rect

import unittest

class testPos(unittest.TestCase):

    def testBaseAssign(self):

        pos=Position(10,10)

        self.assertEqual((pos.x,pos.y),(10,10))

    def testTuple(self):

        pos=Position(10,10)

        self.assertEqual((pos[0],pos[1]),(10,10))

        x=tuple(pos)

        self.assertEqual(x,(10,10))

    def testTupleInit(self):

        pos=Position((10,10))

        self.assertEqual((pos.x,pos.y),(10,10))

    def testErrorInit(self):

        with self.assertRaises(TypeError):
            pos=Position(10,10,10)

    def testEqual(self):

        pos1,pos2=Position(10,10),Position(10,10)

        self.assertEqual(pos1,pos2)

    def testCmp(self):

        pos1,pos2=Position(10,10),Position(4,4)

        assert pos2<pos1

    def testToRect(self):

        pos=Position(20,20)

        rect=pos.toRect(size=(10,20))

        self.assertEqual(rect,Rect((15,10),(25,30)))


       
class testRect(unittest.TestCase):

    def testBaseAssign(self):

        rect=Rect((10,10),(20,20))

        pos1,pos2=Position(10,10),Position(20,20)

        self.assertEqual((rect.lefttop,rect.rightbottom),(pos1,pos2))

    def testEqual(self):

        self.assertEqual(Rect((10,10),(20,20)),Rect((10,10),(20,20)))

    def testTuple(self):

        x=tuple(Rect((10,10),(20,20)))

        self.assertEqual(x,((10,10),(20,20)))

    def testSize(self):
        
        self.assertEqual(Rect((10,10),(30,20)).size,(20,10))

    def testMidPos(self):

        rect=Rect((20,80),(30,40))

        self.assertEqual(rect.getMidPos(),Position(25,60))

    def testSingleGetCollisionPos(self):

        rect=Rect((20,20),(30,30))

        pos=rect.getCollisions(size=(10,10))[0]

        self.assertEqual(pos,Position(25,25))

    def testAnotherGetCollision(self):

        rect=Rect((0,0),(10,10))
        pos=rect.getCollisions(size=(10,10))

        self.assertEqual(len(pos),1)

        pos=pos[0]
        self.assertEqual(pos,Position(5,5))

    def testMultipleGetCollision(self):

        rect=Rect((4,5),(14,15))

        pos=rect.getCollisions(size=(10,10))

        pos=list(sorted(map(tuple,pos)))

        self.assertEqual(pos,[(5,5),(5,15),(15,5),(15,15)])

unittest.main()
